package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.system.Module;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.system.ModuleService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list",name ="展示所有企业数据")
    public String findAll(@RequestParam(name = "page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "2")int pageSize){
       // List<Company> companyList = companyService.findAll();
//       PageBean page=companyService.findPage(pageNum,pageSize);
        PageInfo<Company> pageInfo = companyService.findByPage(pageNum, pageSize);

        request.setAttribute("page",pageInfo);
        return "company/company-list";
    }

    @RequestMapping(value = "/toAdd",name ="进入企业新增页面")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name ="保存企业方法")
    public String edit(Company company){
        if (StringUtils.isEmpty(company.getId())){
            //id为空说明是新增
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        }
        else {
            //id不为空则说明为修改
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }
    @RequestMapping(value = "/toUpdate",name ="进入企业修改页面")
    public String toUpdate(String id){
        Company company=companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-add";
    }
    @RequestMapping(value = "/delete",name ="删除企业信息")
    public String delete(String id){
       companyService.deleteById(id);
        return "redirect:/company/list.do";
    }
    @RequestMapping(value = "/companyModule",name ="进入企业权限分配页面")
    public String companyModule(String companyId){
        Company company = companyService.findById(companyId);
        request.setAttribute("company",company);
        return "company/company-module";
    }
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/getZtreeNodes",name ="获取树形节点数据")
    @ResponseBody
    public List<Map> getZtreeNodes(String companyId){

        //查询此企业所拥有的菜单权限
        List<String> moduleIdList= companyService.findModulesByCompanyId(companyId);
        //查询所有模块数据
        List<Module> moduleList = moduleService.findAll();
        //构造map集合
        List<Map> listMap=new ArrayList<>();
        Map map=null;
        for (Module module : moduleList) {
            map=new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            //判断moduleIdList集合中是否包含module.getId();
            if (moduleIdList.contains(module.getId())){
                map.put("checked",true); //若包含该id  则选中
            }
            listMap.add(map);
        }

        return listMap;
    }

    @RequestMapping(value = "/updateCompanyModule",name ="实现权限分配")
    public String updateCompanyModule(String companyId,String moduleIds){
        companyService.updateCompanyModule(companyId,moduleIds);
        return "redirect:/company/list.do";
    }


}
